<?php

/**
 * @api {get} http://aicomm.bndvip.com/Api/Video/pushVideo/ 获取推流url
 * @apiName pushVideo
 * @apiGroup Video
 *
 * @apiParam {String} user_id 用户id(必填).
 * @apiSuccess {Number} code  返回code(大于0为成功).
 */
/**
 * @api {get} http://aicomm.bndvip.com/Api/Video/videoList/ 直播间列表
 * @apiName videoList
 * @apiGroup Video
 *
 * @apiParam {String} user_id 用户id(必填).
 * @apiSuccess {Number} code  返回code(大于0为成功).
 */
namespace app\api\controller;
use think\Page;
use think\Db;
use think\Model;
use \think\Log;
use app\common\controller\Api;
use Qiniu\Pili\Mac;
use Qiniu\Pili\RoomClient;
use \Qiniu\Pili\Utils;
class Base64Test extends \PHPUnit_Framework_TestCase
{
    public function testUrlSafe()
    {
        $a = '你好';
        $b = Utils::base64UrlEncode($a);
        $this->assertEquals($a, Utils::base64UrlDecode($b));
    }
}

class Video extends Base
{
    private $ak       = "TqmzUSYXlv64puNV5JENIQchWrAEDOs-2IB8_z04";
    private $sk       = "d22f_7xJLGGoHjMwRe-ItC2aNkH70171B9Zu2X3u";
    private $thub       = "aicomm";
    //获取房子列表
    public function getRoom(){
        $mac = new Mac($this->ak, $this->sk);
        $client = new RoomClient($mac);
        $resp=$client->getRoom("testroom");
        print_r($resp);
    }
    //推流地址获取
    public function pushVideo(){
        $user_id = I('post.user_id');
        isset($_SERVER['override_qiniu_auth_time'])? 1234567890: \time();
//        $user_id = 13;
        if (empty($user_id) || $user_id <= 0){
            $return = codemsg(-1);
            Log::api('【Video/pushVideo】-[返回]--'.json_encode($return));
            echo json_encode($return);die;
        }
        //查询是否存在该用户
        $where['user_id'] = array('eq',$user_id);
        $finduser = M('users')->where($where)->find();
        if (empty($finduser)){
            $return = codemsg(-7011);
            Log::api('【Video/pushVideo】-[返回]--'.json_encode($return));
            echo json_encode($return);die;
        }
        //获取token
        $token = $this->getToken();
        $tkey = 'ZHXQ'.$user_id;
        include_once './extend/Qiniu/Pili/Hub.php';
        $_SERVER['override_qiniu_auth_time'] = true;
        $pushUrl = \Qiniu\Pili\RTMPPublishURL("pili-publish.bndvip.com", $this->thub, $tkey ,3600,$this->ak,$this->sk);
        $time = time()+3600;

        $this->assertEquals($pushUrl,"rtmp://pili-publish.bndvip.com/".$this->thub."/".$tkey."?e=".$time."&token=".$token);

        unset($_SERVER['override_qiniu_auth_time']);
        if ($pushUrl){
            $return = codemsg(5003);
            $return['data'] = $pushUrl;
            Log::api('【Video/authList】-[返回]--'.json_encode($return));
            echo json_encode($return);die;
        }else{
            $return = codemsg(-7028);
            Log::api('【Video/authList】-[返回]--'.json_encode($return));
            echo json_encode($return);die;
        }
    }
    public function getToken(){
        $time = time()+3600;
        //这里是/直播应用名/流名?e=当前的时间戳
        $path = "/1314xicong/huxicongp?e=".$time;
        // 计算 HMAC-SHA1 签名，并对签名结果做 URL 安全的 Base64 编码
        $sign=hash_hmac('sha1',$path,$this->sk,true);
        $data=$this->base64urlSafeEncode($sign);
        //   /直播的流名?e=<当前时间>
        $token = $this->ak.":".$data;
        return $token;
    }
    //加签
    function base64urlSafeEncode($data)
    {
        $find = array('+', '/');
        $replace = array('-', '');
        return str_replace($find, $replace, base64_encode($data));
    }
    //拉流,获取播放地址
    public function pullVideo(){
        include_once './extend/Qiniu/Pili/Hub.php';
        $tkey = 'ZHXQ1001';
        $PullUrl = \Qiniu\Pili\RTMPPlayURL("pili-live-rtmp.bndvip.com",$this->thub,$tkey);
        dump($PullUrl);
    }
    //获取直播列表
    public function videoList(){
        $uid = I('post.user_id');
        $where['u.is_lock'] = array('eq',0);
        $list = M('video_room')
                ->alias('v')
                ->field('v.*,u.nickname,u.head_pic')
                ->join('tp_users u','u.user_id=v.uid')
                ->where($where)
                ->select();
        if ($list){
            $return = codemsg(5003);
            $return['data'] = $list;
            Log::api('【video/videoList】-[返回]--'.json_encode($return));
            return json_encode($return);die;
        }else{
            $return = codemsg(-7028);
            Log::api('【video/videoList】-[返回]--'.json_encode($return));
            return json_encode($return);die;
        }
    }

}